/*
  学习目标：setState的第二种写法, （还是异步的语法）
  语法： setState( (旧的state) => 返回的新的state)
  效果：合并：setState会等待上一次的更新结果，再去计算
  目的：减少render执行的次数，
*/
import React from 'react';
export default class App extends React.Component {
  state = {
    count: 0,
  };

  handleClick = () => {
    // const { count } = this.state;
    // 我会被合并执行
    // this.setState({ count: count + 1 });
    // this.setState({ count: count + 2 });
    // this.setState({ count: count + 3 });

    // 通过回调函数，我就不会被合并执行了，而是依次执行
    this.setState((preState) => {
      console.log('preState -----> ', preState);
      return {
        count: preState.count + 1, //0+1=1
      };
    });

    this.setState((preState) => {
      console.log('preState -----> ', preState);
      return {
        count: preState.count + 2, //1+2=3
      };
    });
    this.setState((preState) => {
      console.log('preState -----> ', preState);
      return {
        count: preState.count + 3, //3+3=6
      };
    });
  };

  render() {
    console.log('render -----> ');
    const { count } = this.state;
    return (
      <div>
        <button onClick={this.handleClick}>点我更新count</button>
        <div>我是count - {count}</div>
      </div>
    );
  }
}
